using System.Text.Json;
using System.Text.Json.Nodes;

#if MY_TEST
using Xunit;
public class T01 {
    [Fact]
    public void TestHi0() {
        Assert.Equal(1,1);
    }

[Fact]
public void TestMockChainFromJson() {
    TChain c = TChain.Mock(
        cid:"cid-aaa",
        projectId:"project-aaa",
        projectName:"project aaa",
        p0:"/home/me/repo/work/n3c/bk/T.json" // the template file
    );
    // S.LogInfo($"⚙️ Load TChain = {N.toJson(c)}");
    Assert.Equal(c.L[0].klass, "InitialInput");
    C00_InitialInput i = (C00_InitialInput) c.L[0];
    Assert.Equal(i.projectId, "project-aaa");
    Assert.Equal(i.projectName, "project aaa");
    Assert.Equal(c.cid, "cid-aaa");
}

[Fact]
public void Test02() {
    const string s = "/home/me/repo/work/n3c/front3/src/c02-posts.tmp.json";
    JsonDocument d = JsonDocument.Parse(File.ReadAllText(s));
    List<JsonElement> l = d.RootElement.EnumerateArray().ToList();
    List<TChain> L0 = new List<TChain>();
    List<TChain.Info> L = new List<TChain.Info>();
    for (int i = 0; i < Math.Min(3, l.Count); i++) {
        var c = TChain.Mock(cid:$"cid-{i:000}",
                            projectId:$"project-{i % 5:000}",
                            projectName:l[i].GetProperty("title").GetString() ?? throw new Exception("projectName is null"));
        L0.Add(c);
        L.Add(c.GetInfo());
    }
    S.LogInfo($"🦜 : h = {L0[0].L[0].h}");
    S.LogInfo($"🦜 : creator = {L0[0].L[0].creator}");
    S.LogInfo($"l = {N.toJson(L)}");
    S.LogInfo($"l.Count = {l.Count}");
    Assert.Equal(1,1);
}

[Fact]
public void TestGetChainItem01() {
    // 1. start a mock chain
    var c = new TChain{
        cid = "aaa",L = new List<ChainItem>()
    };
    for (int i = 0; i < 10; i++) {
        c.L.Add(new C04_TestScript{pyscript = $"hi{i}"});
    }
    Assert.Equal(10, c.L.Count);

    // Arrange
    c.L[^2].setGrey();

    // Act
    (int i1, ChainItem? o) = c.findLastNonGreyItem();
    (int i2, ChainItem? o2) = c.findLastNonGreyItem(9);

    // S.LogInfo($"i={i1}, o.h={o?.h}, i2={i2}, o2.h={o2?.h}");
    S.LogInfo($"L[8].h = {c.L[8].h}, L[9].h = {c.L[9].h}");
    // ✅️ L[8].h = ;greyed 2025-07-21T16:38:27.1803288+08:00 🦜: ok

    // Assert
    Assert.Equal(9, i1);
    Assert.NotNull(o);
    Assert.Equal(o.P["pyscript"], "hi9");
    Assert.Equal(7, i2);
    Assert.NotNull(o2);

}


[Fact]
public void TestGetChainItem02() {
    // 1. start a mock chain
    var c = new TChain{cid = "aaa"};
    for (int i = 0; i < 3; i++) {
        c.L.Add(new C04_TestScript{pyscript = $"hi{i}"});
    }
    Assert.Equal(3, c.L.Count);
    c.L[^2].setGrey();
    // Act
    (int i1, ChainItem? o) = c.findLastNonGreyItem(); // last
    (int i2, ChainItem? o2) = c.findLastNonGreyItem(i1); // first
    (int i3, ChainItem? o3) = c.findLastNonGreyItem(i2); // null
    Assert.Equal(2, i1); Assert.NotNull(o); Assert.Equal(o.P["pyscript"], "hi2");
    Assert.Equal(0, i2); Assert.NotNull(o2); Assert.Equal(o2.P["pyscript"], "hi0");
    Assert.Null(o3);
}

[Fact]
public void TestChainClone() {
    // 1. start a mock chain
    var c = new TChain{cid = "aaa"};
    for (int i = 0; i < 3; i++) {
        c.L.Add(new C04_TestScript{pyscript = $"hi{i}"});
    }

    var c2 = (TChain)c.Clone();

    c.L[^2].setGrey();
    Assert.True(c.L[^2].grey);
    Assert.False(c2.L[^2].grey);
}


[Fact]
public void TestChainJson() {
    // 1. start a mock chain
    string p = "/home/me/repo/work/n3c/bk/T.json";
    var c = TChain.FromJson(File.ReadAllText(p));
    Assert.Equal("<mocked-cid>", c.cid);
    var opt = new JsonSerializerOptions {
        IncludeFields = true
    };
    string j = N.toJson(c);
    File.WriteAllText("/home/me/repo/work/n3c/bk/T2.json", j);
}
}
    #endif
